<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像 -->
      <div class="avatar_box">
        <img src="../assets/images/face.png" alt="">
      </div>

      <!-- 登录表单 -->
      <el-form ref="loginForm"
               :model="loginInfo"
               :rules="loginRules"
               label-width="0"
               class="login_form">

        <el-form-item prop="username">

          <el-input v-model="loginInfo.username"
                    :prefix-icon="Avatar"
                    placeholder="请输入用户名">
          </el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input v-model="loginInfo.password"
                    type="password"
                    :prefix-icon="Lock"
                    placeholder="请输入密码">
          </el-input>
        </el-form-item>
        <el-row>
          <el-col :span="14">
            <el-form-item
                class="vcode_item"
                prop="captcha">
              <el-input v-model="loginInfo.captcha" placeholder="请输入验证码"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="10">
            <div class="vcode_img">
              <img :src="imgData" alt="验证码" @click="initCaptcha" title="点击刷新">
            </div>
          </el-col>
        </el-row>
        <el-form-item class="btns">
          <el-button type="primary" @click="loginClick(loginForm)">登录</el-button>
        </el-form-item>
      </el-form>

    </div>
  </div>
</template>

<script setup >

  import { ref, reactive, getCurrentInstance, onMounted } from 'vue';
  import { Avatar, Lock } from "@element-plus/icons-vue";
	import { succesMsg, warnMsg} from "../util/msgBox";
  import { useRouter } from 'vue-router'

	const loginForm = ref(null);
	const { proxy } = getCurrentInstance();

	let loginInfo = reactive({
		username: 'zs',
		password: '123456',
		captchaId: '',
		captcha: ''
	});
	// 验证码图片数据
	let imgData = ref(null);
	const router = useRouter();
	/**
	 * 加载验证码
	 */
	const initCaptcha = () => {
		proxy.$http.get('/user/captcha').then(res => {
			console.log(res);
			let result = res.data;
			imgData.value = result.image;
			loginInfo.captchaId = result.key;
		});
	};

	onMounted(() => {
		initCaptcha()
	});

	// 校验规则
	const loginRules = reactive({
		username: [
			{
				required: true,
				message: '请输入用户名',
				trigger: 'blur',
			}
		],
		password: [
			{
				required: true,
				message: '请输入密码',
				trigger: 'blur',
			},
		]
	});

	/**
	 * 提交登录
	 * @param loginForm
	 */
	const loginClick = (loginForm) => {
		if (!loginForm) return;
		loginForm.validate((valid) => {
			if (valid) {
				proxy.$http.post('/user/login', loginInfo).then(res => {
					console.log(res);
					if (res.code === -1) {
						warnMsg(res.message);
					} else {
						succesMsg(res.message);
						router.push({
							path: '/'
						})
					}
				})

			} else {
				console.log('error submit!');
				return false
			}
		})
	};

</script>

<style lang="less" scoped>
  .login_container {
    background-color: #2b4b6b;
    height: 100%;
  }

  .login_box {
    width: 450px;
    height: 380px;
    background-color: #fff;
    box-shadow: 0 5px 15px rgba(0,0,0,.8);
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

  .avatar_box {
    width: 130px;
    height: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;

  img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    background-color: #eee;
  }
  }

  display: flex;
  justify-content: center;
  }

  .btns {
    display: flex;
    justify-content: center;

  button {
    width: 280px;
    margin-top: 10px;
  }
  }

  .login_form {
    position: absolute;
    bottom: 0;
    width: 75%;
    padding: 20px 30px;
    box-sizing: border-box;
  }

  .vcode_item {
    width: 150px;
    display: flex;
    justify-content: space-between;
  }

  .vcode_img {
    width: 100%;
    height: 40px;
    /*border: 1px #eee solid;*/
    cursor: pointer;
    box-sizing: border-box;
    display: flex;
    justify-content: center;
  }
</style>